package divide;

/**
 * PowxN https://leetcode-cn.com/problems/powx-n/
 *
 * @author <a href="zhaoyingling12@163.com">Zhao Simon</a>
 * @description: 计算x的n次幂
 * @date 2021/6/21
 **/
public class PowxN {

    class Solution1 {
        public double myPow(double x, int n) {
            long N = n;
            return N >= 0 ? quickMul(x, N) : 1.0 / quickMul(x, -N);
        }

        public double quickMul(double x, long N) {
            if (N == 0) {
                return 1.0;
            }
            double y = quickMul(x, N / 2);
            return N % 2 == 0 ? y * y : y * y * x;
        }
    }
}
